/**
 * Copyright (c) 2006, Lukas Eder, OpenWFE.org, EPFL
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * . Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * . Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * . Neither the name of the "OpenWFE" nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id$
 */
package org.dfl.plugin.utils;

import org.dfl.plugin.DflPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;

/**
 * A utility class providing some useful workbench access methods.
 * 
 * @author Lukas Eder
 */
public class WorkbenchUtil {
	/**
	 * Retrieves the currently active org.eclipse.ui.IWorkbenchWindow
	 * 
	 * @return The active part
	 */
	public static IWorkbenchWindow getActiveWindow() {
		IWorkbench workbench = getWorkbench();

		if (workbench != null) {
			return workbench.getActiveWorkbenchWindow();
		}

		return null;
	}

	/**
	 * Retrieves the currently active org.eclipse.ui.IWorkbenchPart
	 * 
	 * @return The active part
	 */
	public static IWorkbenchPart getActivePart() {
		IWorkbenchWindow window = getActiveWindow();

		if (window != null) {
			IWorkbenchPage page = window.getActivePage();

			if (page != null) {
				return page.getActivePart();
			}
		}

		return null;
	}

	/**
	 * Retrieves the currently active org.eclipse.ui.IEditorPart
	 * 
	 * @return The active editor
	 */
	public static IEditorPart getActiveEditor() {
		IWorkbenchWindow window = getActiveWindow();

		if (window != null) {
			IWorkbenchPage page = window.getActivePage();

			if (page != null) {
				return page.getActiveEditor();
			}
		}

		return null;
	}

	public static ISelection getActiveEditorSelection() {
		IEditorPart editor = getActiveEditor();
		if (editor != null) {
			return editor.getSite().getSelectionProvider().getSelection();
		}
		return null;
	}

	public static IViewPart getViewById(String id) {
		IWorkbenchPage page = getActiveWindow().getActivePage();
		if (page != null) {
			IViewReference viewReferences[] = page.getViewReferences();
			for (int i = 0; i < viewReferences.length; i++) {
				if (id.equals(viewReferences[i].getId())) {
					return viewReferences[i].getView(false);
				}
			}
		}
		return null;
	}

	/**
	 * Retrieves an adapter for the currently active
	 * org.eclipse.ui.IWorkbenchPart
	 * 
	 * @param adapter
	 *            The adapter's type
	 * @return An adapter for the active part of the requested type
	 */
	@SuppressWarnings("unchecked")
	public static Object getActivePartAdapter(Class adapter) {
		IWorkbenchPart part = getActivePart();

		if (part != null) {
			return part.getAdapter(adapter);
		}

		return null;
	}

	/**
	 * Puts a message to the statusLine, if available.
	 * 
	 * @param message
	 *            The message.
	 */
	public static void showMessageInStatusBar(String message) {
		IActionBars actionBars = getActiveEditor().getEditorSite()
				.getActionBars();

		if (actionBars != null) {
			IStatusLineManager statusLine = actionBars.getStatusLineManager();

			if (statusLine != null)
				statusLine.setMessage(message);
		}
	}

	/**
	 * returns project workbench
	 * 
	 * @return
	 */
	public static IWorkbench getWorkbench() {
		return DflPlugin.getDefault().getWorkbench();
	}

	public static IProject getCurrentProject() {
		IEditorPart editor = DflPlugin.getDefault().getWorkbench()
				.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		IEditorInput input = editor.getEditorInput();
		IFile file = null;
		if (input instanceof IFileEditorInput) {
			file = ((IFileEditorInput) input).getFile();
		}
		if (file == null)
			return null;

		IProject project = file.getProject();
		return project;

	}
	
	public static void refreshProject(IProject p){
		try {
			p.refreshLocal(IProject.DEPTH_INFINITE, null);
		} catch (CoreException e) {}
	}

	//	
	// public static IProject getActiveProject(){
	// IProject[] projects =
	// ResourcesPlugin.getWorkspace().getRoot().getProjects();
	// projects[0].is
	// }
}